package com.hyy.controller;

import com.hyy.bean.MaskUser;
import com.hyy.service.UserService;
import com.hyy.service.serviceImpl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet(name = "LoginServlet",urlPatterns = "/login")
public class LoginServlet extends HttpServlet {

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		//获取请求参数
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		UserService userService = new UserServiceImpl();
		MaskUser user = userService.login(username,password);

		if(user==null){//用户信息错误，跳转到登录页面
			//需要给出提示信息
			request.setAttribute("msg","用户名不存在或者密码错误！");
			request.getRequestDispatcher("jsp/login.jsp").forward(request,response);
		}else{//用户信息正确的，跳转到首页页面
			request.getSession().setAttribute("user", user);
			request.getRequestDispatcher("/index.jsp").forward(request,response);
		}

	}

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request,response);
	}
}
